package com.hex.ds.hdrs.app.service.impl;

import com.hex.ds.hdrs.app.converter.IAppGroupRelationConverter;
import com.hex.ds.hdrs.app.dao.AppGroupRelationDao;
import com.hex.ds.hdrs.app.dto.AppGroupRelationDto;
import com.hex.ds.hdrs.app.po.AppGroupRelationPo;
import com.hex.ds.hdrs.app.service.IAppGroupRelationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Package com.hex.ds.hdrs.app.service.impl
 * @ClassName AppGroupRelationServiceImpl
 * @Description 应用与应用组管理联业务实现
 * @Author yu liuyang
 * @Date 2023/4/16 10:25
 * @Version v1.0
 **/
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppGroupRelationServiceImpl implements IAppGroupRelationService {

    private final AppGroupRelationDao appGroupRelationDao;

    private final IAppGroupRelationConverter appGroupRelationConverter;


    /**
     * @Method queryListByGroupCode <br>
     * @Param [groupCode] <br>
     * @Return java.util.List<com.hex.ds.hdrs.app.po.AppGroupRelation> <br>
     * @Description 查询应用与应用组关联列表<br>
     * @Author yu liuyang <br>
     * @Date 2023/4/17 14:22 <br>
     * @Version V1.0 <br>
     */
    public List<AppGroupRelationPo> queryListByGroupCode(String groupCode) {
        return appGroupRelationDao.queryList(new AppGroupRelationPo().setGroupCode(groupCode));
    }

    /**
     * @Method queryOneByAppCode <br>
     * @Param [appCode] <br>
     * @Return com.hex.ds.hdrs.app.po.AppGroupRelation <br>
     * @Description 根据应用编码获取组编号<br>
     * @Author yu liuyang <br>
     * @Date 2023/4/17 14:22 <br>
     * @Version V1.0 <br>
     */
    public AppGroupRelationPo queryOneByAppCode(String appCode){
        return appGroupRelationDao.queryOne(new AppGroupRelationPo().setAppCode(appCode));
    }

    /**
     * @Method add <br>
     * @Param [appGroupRelationDto] <br>
     * @Return void <br>
     * @Description 添加应用与应用组关联 <br>
     * @Author yu liuyang <br>
     * @Date 2023/4/16 10:30 <br>
     * @Version V1.0 <br>
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void add(AppGroupRelationDto appGroupRelationDto) {
        AppGroupRelationPo appGroupRelationPo = appGroupRelationConverter.toAppGroupRelation(appGroupRelationDto);
        int count = appGroupRelationDao.insert(appGroupRelationPo);
        if (count != 1) {
            throw new RuntimeException("添加应用与应用组关联失败！");
        }
    }

    /**
     * @Method modifyByAppCode <br>
     * @Param [appGroupRelationDto] <br>
     * @Return void <br>
     * @Description 根据应用编码删除应用与应用组关联 <br>
     * @Author yu liuyang <br>
     * @Date 2023/4/16 10:31 <br>
     * @Version V1.0 <br>
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void modifyByAppCode(AppGroupRelationDto appGroupRelationDto) {
        deleteByAppCode(appGroupRelationDto.getAppCode());
        add(appGroupRelationDto);
    }

    /**
     * @Method deleteByAppCode <br>
     * @Param [appCode] <br>
     * @Return void <br>
     * @Description 根据应用编码删除应用与应用组关联 <br>
     * @Author yu liuyang <br>
     * @Date 2023/4/16 10:31 <br>
     * @Version V1.0 <br>
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void deleteByAppCode(String appCode) {
        int count = appGroupRelationDao.deleteByAppCode(appCode);
        if (count != 1) {
            throw new RuntimeException("删除应用与应用组关联失败！");
        }
    }
}
